package com.hzsai.service.Impl;

import com.alibaba.fastjson.JSON;
import com.hzsai.mappersb.FileMapper;
import com.hzsai.pojo.Actor;
import com.hzsai.service.FileService;
import com.hzsai.utils.FileUtil;
import com.hzsai.utils.OSInfo;
import com.hzsai.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * hzsai 2019/8/22
 * usage:
 **/
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper mapper;

    @Override
    public void downloadUserSQLFile(Integer id, HttpServletRequest request, HttpServletResponse response) {

        try {
            Actor actor = mapper.findById(id);

//            System.out.println("actor :" + id + " " + actor);
//
//            //将取出的信息保存，然后在通过文件下载的方式返回
//            String winTemp = "C:\\Users\\hzsai\\AppData\\Local\\Temp";
//            String linTemp = "/tmp/";
//
            String ori_file = "数据导出"+"Actor_info_" + id + ".sql";
//            String prefix = OSInfo.isWindows() ? winTemp : linTemp ;
//            String filename =  prefix + ori_file;
//            System.out.println("filename: " + filename);
//            OutputStream out = null;
//            try {
//                out = new FileOutputStream(filename);
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//                System.out.println("FileNotFound Exception.");
//            }
            // 写入文件
//            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
//            bufferedOutputStream.write(JSON.toJSONBytes(actor));
//            bufferedOutputStream.close();
//            out.close();

            response.reset();
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            response.setHeader(
                    "content-disposition",
                    String.format("attachment;filename=%s;", URLEncoder.encode(ori_file, "utf-8"))
            );
            response.setHeader("Access-Control-Allow-Origin", "*");
            OutputStream outputStream = response.getOutputStream();
            //
            byte[] actor_data = JSON.toJSONBytes(actor);
            outputStream.write(actor_data);

            outputStream.close();
//            out.close();

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("error...");
        }
    }

    /**
     * 导出文件分为两步:
     * 1. 导出，写入临时文件
     * 2. 返回，将结果以文件下载形式返回客户端
     * @param request
     * @param response
     */
    @Override
    public void exportADB(HttpServletRequest request, HttpServletResponse response) {
        // step 1
        String path = FileUtil.exportDB("springboot");
        System.out.println("Path: " + path);
        if (path != null && path != "") {
            FileUtil.exportToClient(path, request, response);
        }
    }

}
